<?php

namespace app\admin\controller\goods;

use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\response\Json;

/**
 * 商品管理
 *
 * @icon fa fa-circle-o
 */
class Index extends Backend
{

    /**
     * Index模型对象
     * @var \app\admin\model\goods\Index
     */
    protected $model = null;

    /**
     * 是否开启数据限制
     * 支持auth/personal
     * 表示按权限判断/仅限个人
     * 默认为禁用,若启用请务必保证表中存在admin_id字段
     */
    protected $dataLimit = 'personal';

    /**
     * 数据限制字段
     */
    protected $dataLimitField = 'admin_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\goods\Index;

    }


    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->where('admin_id','eq', $this->auth->id)
                ->order($sort, $order)
                ->paginate($limit);




        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }



    /**
     * 添加
     */
    public function add()
    {
        if (false === $this->request->isPost()) {
            return $this->view->fetch();
        }
        $params = $this->request->post("row/a");
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }

        // 添加当前管理员ID
        $params['admin_id'] = $this->auth->id;

        try {
            $result = $this->model->save($params);
            if ($result === false) {
                $this->error($this->model->getError());
            }
            $this->success();
        } catch (think\exception\PDOException $e) {
            $this->error($e->getMessage());
        } catch (think\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        // 检查数据权限
        $currentAdminId = $this->auth->id;
        if ($row[$this->dataLimitField] != $currentAdminId) {
            $this->error(__('You have no permission'));
        }

        if (false === $this->request->isPost()) {
            $this->view->assign("row", $row);
            return $this->view->fetch();
        }

        $params = $this->request->post("row/a");
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }

        // 移除admin_id字段，防止篡改
        unset($params['admin_id']);

        try {
            $result = $row->save($params);
            if ($result === false) {
                $this->error($row->getError());
            }
            $this->success();
        } catch (think\exception\PDOException $e) {
            $this->error($e->getMessage());
        } catch (think\Exception $e) {
            $this->error($e->getMessage());
        }
    }


}
